#!/usr/bin/env python
#coding=utf-8
#dujie

from __future__ import division
import time
import sys
from define import BASE_DIR,BLACK_LIST
sys.path.append(BASE_DIR)

from lib.abstract_cron import abstract_cron
from lib.func_ext import dateTypeInterval,str2sec,sec2str,ksort

#需要修改
class api_hour(abstract_cron) :

    #需要修改
    noLock = True   #是否生成脚本锁文件
#    selectTable = 'sdb_statistics_business_api_hour'
    insertTable = 'sdb_statistics_api_hour'

    def __init__(self) :
        super(api_hour,self).__init__(__file__)


    def boot(self,timestamp=[]) :
#        self.preBoot()

#        keyFilter = self.getTimeFilter(timestamp)
        objCursor = self.getSelectSql()

        if objCursor :
            for v in objCursor :
#                v['hour'] = str2sec(int(v.get('hour','')),'%Y%m%d%H')
                #数据过滤
                self.filterItems(v)

            #批量插入
            self.doBatchExec()
            
        self.sufBoot()
        print 'ok'

    def getSelectSql(self,timestamp=[]) :
        timeFilter = self.getTimeFilter(timestamp)
        sql = 'SELECT `api`,SUM(`sum`) AS sum,SUM(`sum_succ`) AS sum_succ,`date`,`hour`'
        sql += ' FROM `sdb_statistics_business_api_hour`' 
        sql += ' WHERE '+self.db._filter(timeFilter)
        sql += ' GROUP BY `api`'
        return self.db.select(sql)

    def getTimeFilter(self,timestamp=[]) :
        keyFilter = {}
        keyFilter['date'] = timestamp and timestamp or (int(sec2str('%Y%m%d',time.time()-1*24*3600)))
        return keyFilter

    def insertKeys(self) :
        return ['api','sum','sum_succ','date','hour']


#需要修改
if __name__ == '__main__' :
    obj = api_hour()
    obj.boot()

